<template>
  <div class="car">
    <!-- 购物车头部 -->
    <topHeader isFixed>
      <div class="mycar">
        <span class="left">购物车</span>
        <span class="right"
          >领卷
          <span style="color: rgb(14, 4, 4); margin-left: 0.1rem"
            >编辑</span
          ></span
        >
      </div>
    </topHeader>
    
    <!-- 购物车登录界面 -->
    <div class="cartLogin" v-if="!token">
      <!-- 导航 -->
      <div class="nav">
        <div class="item"><i></i> <span>30天无忧退货</span></div>
        <div class="item"><i></i> <span>48小时快速退款</span></div>
        <div class="item"><i></i> <span>满99元免邮费</span></div>
      </div>
      <!-- 登录 -->
      <div class="shopCart">
        <div class="img"></div>
        <!-- <div class="text" @click="$router.push('/personal')">登录</div> -->
        <van-button type="danger" class="text" @click="$router.push('/login')"
          >登录</van-button
        >
        <!-- <div class="text2" style="font-size: .4rem;">添加点儿什么吧</div> -->
      </div>
    </div>

    <!-- 购物车列表 -->
    <div class="carList" v-else>
      <!-- 头部导航 -->
      <div class="qianggou">
        <van-notice-bar text="自营抢购,再购44元免邮" />
      </div>

      <!-- 全场换购 -->

      <!-- 商品列表 -->

      <div class="goodList">
        <!-- 商家 -->
        <div class="shopList" v-for="item in cartList" :key="item.id">
          <!-- 商家上面头部介绍 -->
          <div class="shopTop">
            <!-- 商家上面的多选框 -->
            <!-- <van-checkbox
              v-model="item.isChecked"
              isChecked-color="#ee0a24"
              style="padding-left: 15px"
              @change="handlerCheckOne(item, $event)"
            ></van-checkbox> -->
            <van-notice-bar
              class="shopTopRight"
              color="990000"
              background="#fff"
              left-icon="info-o"
            >
              {{ item.name }}
            </van-notice-bar>
          </div>
          <!-- 商家的每一个商品 -->
          <van-swipe-cell class="gooditem">
            <!-- 商品内容 -->
            <template #default>
              <div class="itemInfo">
                <van-checkbox
                  v-model="item.isChecked"
                  isChecked-color="#ee0a24"
                  style="padding-left: 20px"
                  @change="handlerCheckOne(item, $event)"
                ></van-checkbox>
                <div class="intemInfoRight">
                  <div class="title">人体工学椅旗舰店</div>
                  <div class="center">
                    <img :src="item.primaryPicUrl" alt="" />
                    <div class="centerRight">
                      <div class="centerRightTitle">
                        <span>特价</span>
                        <span>{{ item.name }}</span>
                      </div>

                      <van-tag size="medium" type="warning">
                        淡黄色; M（165/84A）<van-icon name="arrow-down" />
                      </van-tag>
                      <div class="centerRightPrice">
                        <span>￥{{ item.retailPrice }}元 </span>
                        <!-- <span>￥60</span> -->
                        <van-stepper
                          style="margin-left: 60px"
                          step="1"
                          v-model="item.count"
                          min="0"
                          max="200"
                          integer
                          :name="
                            item.count +
                            ':' +
                            item.retailPrice +
                            ':' +
                            item.isChecked +
                            ':' +
                            item.productId
                          "
                          @change="handlercount"
                        />
                      </div>
                    </div>
                  </div>
                  <div class="bottom">
                    <van-icon name="fire-o" />
                    <span>自营商场商品满99免邮</span>
                  </div>
                </div>
              </div>
            </template>
            <!-- 商品右边拉出来 -->
            <template #right>
              <van-button
                square
                text="删除"
                type="danger"
                class="delete-button"
              />
            </template>
          </van-swipe-cell>
        </div>
      </div>

      <!-- 下单按钮 -->
      <div class="orederBottom">
        <van-submit-bar
          :price="totalPrice * 100"
          button-text="下单"
          @submit="onSubmit"
          safe-area-inset
          @tabbar-item-icon-size="0"
        >
          <van-checkbox
            :value="isAll"
            ref="checkboxGroup"
            @click="handlerCheckAll($event)"
            >全选</van-checkbox
          >
          <template #tip>
            你的收货地址不支持同城送,
            <span @click="onClickEditAddress">修改地址</span>
          </template>
        </van-submit-bar>
        <div style="height: 1.7rem"></div>
      </div>
    </div>
  </div>
</template>
<script>
import {
  getShopCartList,
  changeCartOnecheck,
  changeCartCount,
} from '../../axios'
import { Toast } from 'vant'
export default {
  name: 'carT',
  data() {
    return {
      isShow: false,
      cartList: [],
      token: '',
      totoPrice: 0,
    }
  },
  computed: {
    isAll() {
      return this.cartList.every((item) => {
        return item.isChecked
      })
    },
    totalPrice() {
      return this.cartList.reduce((pre, item) => {
        return pre + (item.isChecked ? item.count * item.retailPrice * 1 : 0)
      }, 0)
    },
  },
  mounted() {
    this.getcartList()
    this.hasToken()
    this.totalPrice = this.totalPrice()
    // console.log(totalPrice)
  },
  methods: {
    //一挂载就赋值属性

    //获取token,判断是否登录
    hasToken() {
      this.token = localStorage.getItem('token')
    },
    onClickEditAddress() {},
    // 点击跳转提交购物车
    onSubmit() {
      this.cartList.filter((item) => {
        if (item.isChecked && item.count > 0) {
          this.$router.push('/orderCart')
          // let  cartList  = this.cartList
          // let  totalPrice  = this.totalPrice
          // const cartLIstData={cartList,totalPrice}
          // console.log(this.cartList)
          // console.log(this.totalPrice)

          this.$bus.$emit('1', this.cartList)
          this.$bus.$emit('2', this.totalPrice)
          return
        } else {
          alert('请选择一个需要购买的物品')
        }
      })
    },
    async getcartList() {
      const result = await getShopCartList()

      console.log(result)
      this.cartList = result.data
    },

    //数量增加事件
    handlercount(newValue, { name }) {
      // console.log(newValue,name)
      let [oldValue, price, isChecked, productId] = name.split(':')
      console.log(oldValue, newValue, price, isChecked, productId)

      Toast.loading({ forbidClick: true })

      clearTimeout(this.timer)
      this.timer = setTimeout(async () => {
        Toast.clear()
        // 注意此时修改 value 后会再次触发 change 事件
        const data = {
          productId,
          // num: - 1,
          num: parseInt(newValue) > parseInt(oldValue) ? +1 : -1,
        }
        const result = await changeCartCount(data)
      }, 500)

      //调用接口修改选择值
      // const data = {
      //   productId,
      //   // num: - 1,
      //   num: parseInt(newValue) > parseInt(oldValue) ? +1 : -1,
      // }
      // const result = await changeCartCount(data)
      if (result.code === 200) {
        // let a =0
        const totoPrice = this.cartList.reduce((pre, item) => {
          return pre + (item.isChecked ? item.count * item.retailPrice * 1 : 0)
        }, 0)
        this.totoPrice = parseInt(totoPrice * 100)
        console.log(this.totoPrice)
      }
    },

    // 全选全不选
    async handlerCheckAll(e) {
      const isChecked = this.isAll ? 0 : 1
      if (isChecked) {
        this.cartList.forEach((item) => (item.isChecked = isChecked))
      } else {
        this.cartList.forEach((item) => (item.isChecked = isChecked))
      }
    },
    // 单选
    async handlerCheckOne(item, e) {
      // console.log(item, 11111)
      let { productId, isChecked } = item
      console.log(productId)
      // console.log(item,isChecked, isChecked === 1,isChecked===0)
      const data = { productId, isChecked: +isChecked }
      console.log(data)
      //
      const result = await changeCartOnecheck(data)
      console.log(data)
      console.log(result)
      // console.log(result)
      if (result.code === 200) {
        // let a =0
        const totoPrice = this.cartList.reduce((pre, item) => {
          return pre + (item.isChecked ? item.count * item.retailPrice * 1 : 0)
        }, 0)
        this.totoPrice = parseInt(totoPrice * 100)
        console.log(this.totoPrice)
      }
    },
  },
}
</script>
<style lang="less">
// 底部购物车样式
.van-submit-bar {
  bottom: 1.3rem;
  border-bottom: 0.1 solid #666;
}
.car {
  /* 购物车头部 */
  width: 100%;
  height: 100%;

  .mycar {
    display: flex;
    justify-content: space-between;
    height: 1.17333rem;
    padding: 0 0.6rem 0 0.4rem;
    line-height: 1.17333rem;
    text-align: center;
    background-color: rgb(245, 240, 240);

    .left {
      font-size: 0.5rem;
    }
    .right {
      font-size: 0.4rem;
      color: rgb(207, 13, 13);
    }
  }
  .cartLogin {
    /* 导航 */
    .nav {
      display: flex;
      justify-content: space-around;
      background: #eee;
      height: 0.9333rem;
      .item {
        display: flex;
        align-items: center;
        font-size: 0.32rem;
        color: #666;
      }
      i {
        width: 0.1rem;
        height: 0.1rem;
        border-radius: 50%;
        border: 2px solid #bbb;
        margin-right: 0.13rem;
      }
    }
    /* 登录 */
    .shopCart {
      position: relative;
      height: 100%;
      background-color: #f4f4f4;
      .img {
        position: absolute;
        top: 4rem;
        left: 50%;
        transform: translateX(-50%);
        background-image: url('../cart/images/caricon.webp');
        display: inline-block;
        vertical-align: middle;
        width: 3.30667rem;
        height: 3.30667rem;
        margin-bottom: 0.10667rem;
        background-size: 3.30667rem 3.30667rem;
        background-position: center center;
        background-repeat: no-repeat;
      }
      .text {
        position: absolute;
        top: 7.4rem;
        left: 50%;
        transform: translateX(-50%);
        // margin: auto;
        width: 6.4rem;
        // height: 1.22667rem;
        // font-size: .4rem;
        // line-height: 1.22667rem;
        // color: #fff;
        // // background-color: #dd1a21;
        // border-radius: .01rem;
        // text-align: center;
      }
      .text2 {
        position: absolute;
        top: 7rem;
        left: 50%;
        transform: translateX(-50%);
        margin: auto;
        width: 6.4rem;
        height: 1.22667rem;
        font-size: 0.4rem;
        line-height: 1.22667rem;
        text-align: center;
        color: #666;
      }
    }
  }
  .carList {
    // margin-bottom: -200px;
    .qianggou {
      height: 1rem;
    }
    .cartListheader {
      // font-size: 0.4rem;
      box-sizing: border-box;
      padding: 0 0.4rem;
      height: 1rem;
      position: relative;
      .left {
        padding-top: 2rem;
        line-height: 1.3rem;

        font-size: 0.3rem;
      }
      .right {
        text-align: right;

        line-height: 1.3rem;
        font-size: 0.2rem;
        color: red;
      }

      .bottom {
        font-size: 0.24rem;
        margin: 0 0 0.1rem 0.86rem;
        text-align: right;
        background-image: linear-gradient(
          -225deg,
          rgba(252, 134, 95, 0.05),
          rgba(252, 98, 95, 0.05) 100%
        );
        line-height: 0.56rem;
      }
    }
    .goodList {
      background-color: #fff;
      // padding-bottom: 1.5rem;

      .shopList {
        // margin-bottom: 0.01px;
        overflow: hidden;
        .shopTop {
          display: flex;
          .shopTopRight {
            height: 1rem;
            flex: 1;
            margin-bottom: 0.1rem;
          }
        }
        .gooditem {
          .itemInfo {
            display: flex;
            .intemInfoRight {
              width: 78%;
              height: 4.29rem;
              .title {
                font-size: 0.32rem;
                padding-top: 0.16rem;
                padding-left: 1.5rem;
              }
              .center {
                display: flex;
                img {
                  width: 2.29rem;
                  height: 2.29rem;
                }
                .centerRight {
                  .centerRightTitle {
                    font-size: 0.37rem;
                  }
                  .centerRightPrice {
                    display: flex;
                    span {
                      flex: 1;
                      font-size: 0.32rem;
                      color: red;
                    }
                  }
                }
              }
              .bottom {
                font-size: 0.32rem;
                color: #ccc;
                margin-left: 15px;
                border-bottom: 0.1rem solid #eee;
                width: 100%;
              }
            }
          }
        }
      }

      .goods-card {
        margin: 0;
        background-color: white;
        height: 2rem;
      }

      .delete-button {
        height: 100%;
      }
    }
  }

  .van-notice-bar__wrap {
    height: 1rem;
  }
}
</style>
